home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / dev / gcc / gcc263_utils.lha / gnu / bin / updatedb < prev    next >
Text File  |  1994-12-08  |  4KB  |  149 lines

  1. #!/bin/sh
  2. # updatedb -- build a locate pathname database
  3. # Copyright (C) 1994 Free Software Foundation, Inc.
  4.  
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9.  
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14.  
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. # csh original by James Woods; sh conversion by David MacKenzie.
  20.  
  21. usage="\
  22. Usage: updatedb [--localpaths='dir1 dir2...'] [--netpaths='dir1 dir2...']
  23.        [--prunepaths='dir1 dir2...'] [--output=dbfile] [--netuser=user]
  24.        [--old-format] [--version] [--help]"
  25.  
  26. old=no
  27. for arg
  28. do
  29.   opt=`echo $arg|sed 's/^\([^=]*\).*/\1/'`
  30.   val=`echo $arg|sed 's/^[^=]*=\(.*\)/\1/'`
  31.   case "$opt" in
  32.     --localpaths) SEARCHPATHS="$val" ;;
  33.     --netpaths) NETPATHS="$val" ;;
  34.     --prunepaths) PRUNEPATHS="$val" ;;
  35.     --output) LOCATE_DB="$val" ;;
  36.     --netuser) NETUSER="$val" ;;
  37.     --old-format) old=yes ;;
  38.     --version) echo "GNU updatedb version 4.1"; exit 0 ;;
  39.     --help) echo "$usage"; exit 0 ;;
  40.     *) echo "updatedb: invalid option $opt
  41. $usage" >&2
  42.        exit 1 ;;
  43.   esac
  44. done
  45.  
  46. # You can set these in the environment, or use command-line options,
  47. # to override their defaults:
  48.  
  49. # Non-network directories to put in the database.
  50. : ${SEARCHPATHS="/"}
  51.  
  52. # Network (NFS, AFS, RFS, etc.) directories to put in the database.
  53. : ${NETPATHS=}
  54.  
  55. # Directories to not put in the database, which would otherwise be.
  56. : ${PRUNEPATHS="/tmp /usr/tmp /var/tmp /afs"}
  57.  
  58. # The same, in the form of a regex that find can use.
  59. test -z "$PRUNEREGEX" &&
  60.   PRUNEREGEX=`echo $PRUNEPATHS|sed -e 's,^,\\\(^,' -e 's, ,$\\\)\\\|\\\(^,g' -e 's,$,$\\\),'`
  61.  
  62. # The database file to build.
  63. : ${LOCATE_DB=/gnu/var/locatedb}
  64.  
  65. # Directory to hold intermediate files.
  66. if test -d /var/tmp; then
  67.   : ${TMPDIR=/var/tmp}
  68. else
  69.   : ${TMPDIR=/usr/tmp}
  70. fi
  71.  
  72. # The user to search network directories as.
  73. : ${NETUSER=daemon}
  74.  
  75. # The directory containing the subprograms.
  76. : ${LIBEXECDIR=/gnu/libexec}
  77.  
  78. # The directory containing find.
  79. : ${BINDIR=/gnu/bin}
  80.  
  81. # The names of the utilities to run to build the database.
  82. : ${find=find}
  83. : ${frcode=frcode}
  84. : ${bigram=bigram}
  85. : ${code=code}
  86.  
  87. PATH=$LIBEXECDIR:$BINDIR:/usr/ucb:/bin:/usr/bin:$PATH export PATH
  88.  
  89. # Make and code the file list.
  90. # Sort case insensitively for users' convenience.
  91.  
  92. if test $old = no; then
  93.  
  94. # FIXME figure out how to sort null-terminated strings, and use -print0.
  95. {
  96. if test -n "$SEARCHPATHS"; then
  97.   $find $SEARCHPATHS \
  98.   \( -fstype nfs -o -fstype NFS -o -type d -regex "$PRUNEREGEX" \) -prune -o -print
  99. fi
  100.  
  101. if test -n "$NETPATHS"; then
  102.   su $NETUSER -c \
  103.   "$find $NETPATHS \\( -type d -regex \"$PRUNEREGEX\" -prune \\) -o -print"
  104. fi
  105. } | sort -f | $frcode > $LOCATE_DB.n
  106.  
  107. # To avoid breaking locate while this script is running, put the
  108. # results in a temp file, then rename it atomically.
  109. if test -s $LOCATE_DB.n; then
  110.   rm -f $LOCATE_DB
  111.   mv $LOCATE_DB.n $LOCATE_DB
  112.   chmod 644 $LOCATE_DB
  113. else
  114.   echo "updatedb: new database would be empty" >&2
  115.   rm -f $LOCATE_DB.n
  116. fi
  117.  
  118. else # old
  119.  
  120. bigrams=$TMPDIR/f.bigrams$$
  121. filelist=$TMPDIR/f.list$$
  122. trap 'rm -f $bigrams $filelist; exit' 1 15
  123.  
  124. # Alphabetize subdirectories before file entries using tr.  James says:
  125. # "to get everything in monotonic collating sequence, to avoid some
  126. # breakage i'll have to think about."
  127. {
  128. if test -n "$SEARCHPATHS"; then
  129.   $find $SEARCHPATHS \
  130.   \( -fstype nfs -o -fstype NFS -o -type d -regex "$PRUNEREGEX" \) -prune -o -print
  131. fi
  132. if test -n "$NETPATHS"; then
  133.   su $NETUSER -c \
  134.   "$find $NETPATHS \\( -type d -regex \"$PRUNEREGEX\" -prune \\) -o -print"
  135. fi
  136. } | tr / '\001' | sort -f | tr '\001' / > $filelist
  137.  
  138. # Compute the (at most 128) most common bigrams in the file list.
  139. $bigram < $filelist | sort | uniq -c | sort -nr |
  140.   awk '{ if (NR <= 128) print $2 }' | tr -d '\012' > $bigrams
  141.  
  142. # Code the file list.
  143. $code $bigrams < $filelist > $LOCATE_DB
  144. chmod 644 $LOCATE_DB
  145.  
  146. rm -f $bigrams $filelist $errs
  147.  
  148. fi
  149.